package com.beanutil.examples;

import java.lang.reflect.InvocationTargetException;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;


public class BeanUtilExample1 {

	public static void main(String[] args) {
		BeanUtilExample1 example=new BeanUtilExample1();
		User user=example.prepareDate();
		try {
	/*-----------分割线-----------------------
			System.out.println(BeanUtils.getProperty(user, "userId"));
			//BeanUtils.getProperty()方法返回String类型
			//System.out.println(PropertyUtils.getProperty(user, "userId"));
			//PropertyUtils.getProperty()方法返回Object类型
			System.out.println(BeanUtils.getProperty(user, "username"));
			System.out.println(BeanUtils.getProperty(user, "password"));
			System.out.println(BeanUtils.getProperty(user, "profile.email"));
			System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));
			System.out.println("home:"+BeanUtils.getProperty(user, "profile.phone(home)"));
			System.out.println("office:"+BeanUtils.getProperty(user, "profile.phone(office)"));
			System.out.println("mobile:"+BeanUtils.getProperty(user, "profile.phone(mobile)"));
			System.out.println(BeanUtils.getProperty(user, "profile.address[0].city"));
			System.out.println(BeanUtils.getProperty(user, "profile.address[1].city"));
			System.out.println(BeanUtils.getProperty(user, "profile.address[2].city"));
		-----------分割线-----------------------*/
			User user2=new User();
			BeanUtils.copyProperties(user2, user);
			//复制javabean
			BeanUtils.setProperty(user2, "userId", new Long(999));
			BeanUtils.setProperty(user2, "username", "xxxxaaaa");
			BeanUtils.setProperty(user2, "profile.email", "daohaogun@152.com");
			BeanUtils.setProperty(user2, "profile.birthDate", new GregorianCalendar(1000,1,1).getTime());
			BeanUtils.setProperty(user2, "profile.address[0]", new Address("英国","伦敦","xxxx","2312"));
			//对比user和user2对象的属性值发现：
			//这样的copy属于浅层copy
			//碰到基本类型或String类型的copy值，碰到引用类型的copy地址
			System.out.println("=============USER===========");
			System.out.println(BeanUtils.getProperty(user, "userId"));
			System.out.println(BeanUtils.getProperty(user, "username"));
			System.out.println(BeanUtils.getProperty(user, "password"));
			System.out.println(BeanUtils.getProperty(user, "profile.email"));
			System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));
			System.out.println("home:"+BeanUtils.getProperty(user, "profile.phone(home)"));
			System.out.println("office:"+BeanUtils.getProperty(user, "profile.phone(office)"));
			System.out.println("mobile:"+BeanUtils.getProperty(user, "profile.phone(mobile)"));
			System.out.println(BeanUtils.getProperty(user, "profile.address[0].city"));
			System.out.println(BeanUtils.getProperty(user, "profile.address[1].city"));
			System.out.println(BeanUtils.getProperty(user, "profile.address[2].city"));
			/*-----------------------分割线-----------------------*/
			System.out.println("=============USER2===========");
			System.out.println("userId:"+BeanUtils.getProperty(user2, "userId"));
			System.out.println("username:"+BeanUtils.getProperty(user2, "username"));
			System.out.println("password:"+BeanUtils.getProperty(user2, "password"));
			System.out.println("email:"+BeanUtils.getProperty(user2, "profile.email"));
			System.out.println("birthDate:"+BeanUtils.getProperty(user2, "profile.birthDate"));
			System.out.println("home:"+BeanUtils.getProperty(user2, "profile.phone(home)"));
			System.out.println("office:"+BeanUtils.getProperty(user2, "profile.phone(office)"));
			System.out.println("mobile:"+BeanUtils.getProperty(user2, "profile.phone(mobile)"));
			System.out.println("city:"+BeanUtils.getProperty(user2, "profile.address[0].city"));
			System.out.println("city:"+BeanUtils.getProperty(user2, "profile.address[1].city"));
			System.out.println("city:"+BeanUtils.getProperty(user2, "profile.address[2].city"));
			
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
	private User prepareDate(){
		Profile profile=new Profile();
		profile.setEmail("klasjd@163.com");
		profile.setBirthDate(new GregorianCalendar(2020,9,10).getTime());
		Map<String, String>phone = new HashMap<String, String>();
		phone.put("home","1231223");
		phone.put("office", "443422");
		phone.put("mobile", "12344232123");
		profile.setPhone(phone);
		Address a1=new  Address("中国", "北京", "惺惺惜惺惺小", "201231");
		Address a2=new  Address("中国", "上海", "万维网往往", "221231");
		Address a3=new  Address("中国", "沈阳", "的弹道导弹小", "231231");
		Address[] address={a1,a2,a3};
		profile.setAddress(address);
		User user=new User();
		user.setUserId(new Long(10001));
		user.setUsername("jqblizzie");
		user.setPassword("adsdsadas");
		user.setProfile(profile);
		return user;
	}
}




